package com.hs.controller;


import com.hs.entity.Course;
import com.hs.entity.Major;
import com.hs.entity.Result;
import com.hs.service.CourseService;
import com.hs.service.MajorService;
import com.hs.service.SequenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private MajorService majorService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private SequenceService sequenceService;

    @RequestMapping("/courseList")
    public String courseList(){
        return "manager/course-list";
    }

    @RequestMapping("/list/course")
    @ResponseBody
    public Map userListJson(String searchKey){
        List<Course> list=courseService.findCourseList("%"+searchKey+"%");
        Map<String,Object> map=new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",list.size());
        map.put("data",list);
        return map;
    }


    @RequestMapping("/list/add")
    public String userAdd(Model model){
        List<Major> list=majorService.queryMajorList();
        model.addAttribute("majorInfo",list);
        return "manager/course-add";
    }

    @RequestMapping("/addcourse")
    @ResponseBody
    public Result addCourse(String name, String remark,String modules){
        Course course=new Course();
        course.setName(name);
        course.setRemark(remark);
        course.setMajid(modules);
        Integer row=courseService.addCourse(course);
        Result result=new Result();
        if(row>0){
            result.setCode(0);
            result.setMsg("添加成功");
        }else {
            result.setCode(10);
            result.setMsg("添加失败");
        }

        return result;
    }

    @ResponseBody
    @RequestMapping("/deleteCourseById")
    public Map deleteBookById(Integer id){
        int i=courseService.delCourseById(id);
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count","");
        map.put("data",i);
        return map;
    }
}
